package m202401.d07;

/**
 * @@author: 爱做梦的锤子
 * @date: 2024/1/7 09:16
 */
//19. 删除链表的倒数第 N 个结点
public class Q19 {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        int flag = this.handle(head, n);
        if (flag == n) {
            return head.next;
        }
        return head;
    }

    private int handle(ListNode head, int n) {
        if (head == null) {
            return 0;
        }
        int flag = 1 + handle(head.next, n);
        if (flag == n + 1) {
            head.next = head.next.next;
        }
        return flag;
    }

    static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
            next = null;
        }
    }
}
